package com.example.smartf.service.impl;

import com.example.smartf.model.mysql.Deviceinfo;
import com.example.smartf.mqtt.DownHandler;
import com.example.smartf.repository.mysql.DeviceinfoRepository;
import com.example.smartf.service.DevService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DevServiceImpl implements DevService {
    //日志记录器
    Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private DeviceinfoRepository deviceinfoRepository;

    //向设备发送信息
    public String DownCtrl(String username, String deviceID, String msg){
        DownHandler downHandler = new DownHandler();
        logger.info("设备操作：" + " 操作设备：" + deviceID + "下行控制指令：" + msg);
        return downHandler.sendDownMesg(deviceID,username,msg);
    }

    //查询设备阈值
    public Deviceinfo Getthreshold(String deviceID){
        Deviceinfo deviceinfo = deviceinfoRepository.findByDeviceid(deviceID);
        return deviceinfo;
    }

    //更改设备阈值
    public String Updatethreshold(String deviceID, String type, Double max , Double min){
        switch (type){
            case "co2" :
                deviceinfoRepository.updateCO2threshold(deviceID,max,min);
                break;
            case "ec" :
                deviceinfoRepository.updateECthreshold(deviceID,max,min);
                break;
            case "light":
                deviceinfoRepository.updateLIGHTthreshold(deviceID,max,min);
                break;
            case "ph" :
                deviceinfoRepository.updatePHthreshold(deviceID,max,min);
                break;
            case "rh" :
                deviceinfoRepository.updateRHthreshold(deviceID,max,min);
                break;
            case "temp" :
                deviceinfoRepository.updateTEMPthreshold(deviceID,max,min);
                break;
            default:
                logger.warn("设备操作：" + " 操作设备：" + deviceID + "阈值更改：" + type + " type 参数异常！");
                return "code error";

        }

        logger.info("设备操作：" + " 操作设备：" + deviceID + "阈值更改：" + type + " 上限：" + max +  " 下限：" + min);
        return "success";
    }
}
